#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu May 23 13:27:53 2019

@author: gloria

GRAPH A1
"""
# PACKAGES #############################################################

import pandas as pd
import os
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib import rcParams


# COMPUTER #############################################################

computer = 'glgennaro'  # work
# computer = 'gloria' #personal


# SETTINGS          #####################################################
# figure size in inches
rcParams['figure.figsize'] = 11.7,8.27


# PRESIDENTIAL 2016 #####################################################

os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/dta')
df = pd.read_stata('presidential_election_complete.dta')

plt.clf()


sns.distplot(df['pop_dim2'], norm_hist = True, rug = False, kde = False, color="black", 
            bins=20)

df['candidate'][df['candidate']=='trump']='Trump'
df['candidate'][df['candidate']=='clinton']='Clinton'

l = 'Trump'
subset = df[df['candidate'] == l]
sns.distplot(subset['pop_dim2'], hist = False, rug = False, kde = True, color="black", 
             kde_kws = {'linewidth': 2, 'linestyle':'--'}, label=l)
    
l = 'Clinton'
subset = df[df['candidate'] == l]
sns.distplot(subset['pop_dim2'], hist = False, rug = False, kde = True, color="black", 
             kde_kws = {'linewidth': 2}, label=l)
    

plt.title('Presidential Campaign 2016',size=35)
plt.xlabel('Populism', size=30)
plt.legend(fontsize='xx-large')

os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/Replication packages/RP_2020_v2/Graphs')
plt.savefig('kden_pres.png')

plt.clf()

# CONGRESS 2018 #####################################################

os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/dta')
df = pd.read_stata('congress_election_complete.dta')

sns.distplot(df['pop_dim2'], norm_hist = True, rug = False, kde = False, color="black", 
            bins=20)

df['outsider'][df['outsider']==1]='Outsider'
df['outsider'][df['outsider']==0]='Insider'
  
l = 'Outsider'
subset = df[df['outsider'] == l]
sns.distplot(subset['pop_dim2'], hist = False, kde = True, color="black", 
             kde_kws = {'linewidth': 2, 'linestyle':'--'}, label=l)
    
l = 'Insider'
subset = df[df['outsider'] == l]
sns.distplot(subset['pop_dim2'], hist = False, kde = True, color="black", 
             kde_kws = {'linewidth': 2}, label=l)
    
plt.title('Congressional Campaign 2018', size=35)
plt.xlabel('Populism', size=30)
plt.legend(fontsize='xx-large')


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/Replication packages/RP_2020_v2/Graphs')
plt.savefig('kden_congress2018.png')

plt.clf()

# CONGRESS 2020 #####################################################

os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/dta')
df = pd.read_stata('congress_election_complete2020.dta')

sns.distplot(df['pop_dim2'], norm_hist = True, rug = False, kde = False, color="black", 
            bins=20)

df['outsider2'][df['outsider2']==1]='Outsider'
df['outsider2'][df['outsider2']==0]='Insider'
  
l = 'Outsider'
subset = df[df['outsider2'] == l]
sns.distplot(subset['pop_dim2'], hist = False, kde = True, color="black", 
             kde_kws = {'linewidth': 2, 'linestyle':'--'}, label=l)
    
l = 'Insider'
subset = df[df['outsider2'] == l]
sns.distplot(subset['pop_dim2'], hist = False, kde = True, color="black", 
             kde_kws = {'linewidth': 2}, label=l)
    
plt.title('Congressional Campaign 2020', size=35)
plt.xlabel('Populism', size=30)
plt.legend(fontsize='xx-large')


os.chdir('/Users/' + computer + '/Dropbox/Progetti/Rhetoric/Replication packages/RP_2020_v2/Graphs')
plt.savefig('kden_congress2020.png')
plt.clf()
